package com.sxfq.loansupermarket.backsystem.modular.refunds.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.refunds.dao.PaymentTotalDao;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.PaymentTongJi;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.PaymentTotalInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchPaymentTotalInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.service.IPaymentTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PaymentTotalServiceImpl implements IPaymentTotalService {

    @Autowired
    private PaymentTotalDao dao;

    @Autowired
    ICustomerService customerService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<PaymentTotalInfo> selectPageListInfo(Page<PaymentTotalInfo> page, SearchPaymentTotalInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
//        if (StringUtil.isNotEmpty(searchInfo.getCheckoutMethod())) {
//            params.put("checkoutMethod", searchInfo.getCheckoutMethod());
//        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("doctor", searchInfo.getCustomerStatus());
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        params.put("tableName", this.getTableName());
        params.put("tableName1", tableName1);
        return dao.getQueryPage(page, params);
    }

    public List<Map<String, Object>> list() {
//        String sql = "select id, outpatient_name,abbreviation from outpatient_management_info order by  num asc;";
        String sql = "select id,checkout_method from checkout_method_info";
        return sqlMapper.selectList(sql);
    }

    public List<PaymentTongJi> paymentRecordExport(PaymentTongJi info) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        String tableName2 = tableHead + "deal_info";
        String sql = "select a.customer_id, b.customer_name , b.medical_record_number ,a.document_number, b.field_tc,c.tc_name,c.doctor," +
                "   c.nurse,b.one_channel, concat('A',c.left_up,'B',c.right_up,'C',c.right_down,'D',c.left_down) as 'yawei',d.department," +
                "   c.left_up as leftUp,c.right_up as rightUp,c.right_down as rightDown,c.left_down as leftDown," +
                "   d.pay_service_name, d.department,d.pay_service_name ,c.folding_pay/(d.pay_service_price_two*c.pay_service_num) as discount," +
                "   sum(a.actual_account_balance) as actualAccountBalance, " +
                "   (" +
                "       CASE b.customer_sex" +
                "       WHEN 1 THEN" +
                "       '男'" +
                "       WHEN 2 THEN" +
                "       '女'" +
                "       ELSE" +
                "       '未知'" +
                "       END" +
                "   ) as customerSex ,b.customer_age,  a.id2,\n" +
                "   sum(case when a.checkout_method='现金' then  a.actual_account_balance else 0 end) as cash ,\n" +
                "   sum(case when a.checkout_method='扫码支付' then a.actual_account_balance else 0 end) as scavenging,\n" +
                "   sum(case when a.checkout_method='美团' then a.actual_account_balance else 0 end) as meiTuan,\n" +
                "   sum(case when a.checkout_method='分期贷款' then a.actual_account_balance else 0 end) as fqdk,\n" +
                "   sum(case when a.checkout_method='VIP消费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   (select  arrears  from " + tableName2 + " where c.id2=id2 and deal_status='已成交' order by get_time desc limit 0,1) as arrears,\n" +
                "   sum(case when a.checkout_method='分期收费' then a.actual_account_balance else 0 end) as fqsf,\n" +
                "   sum(case when a.checkout_method='VIP收费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   sum(case when a.checkout_method='积分抵扣' then a.actual_account_balance else 0 end) as integralDeduction,\n" +
                "   sum(case when a.checkout_method='华荣时代' then a.actual_account_balance else 0 end) as huaRong, \n" +
                "   sum(case when a.checkout_method='代金券' then a.actual_account_balance else 0 end) as voucher,\n" +
                "   sum(case when a.checkout_method='医保' then a.actual_account_balance else 0 end) as medicalInsurance,\n" +
                "   sum(case when a.checkout_method='糯米' then a.actual_account_balance else 0 end) as nuoMi ," +
                "   sum(case when a.checkout_method='银行卡' then a.actual_account_balance else 0 end) as payByCard, " +
                "   sum(case when a.checkout_method='百度钱包' then a.actual_account_balance else 0 end) as baiduqb, " +
                "   sum(case when a.checkout_method='社保卡' then a.actual_account_balance else 0 end) as shebao, a.recharge_people," +
                "   b.customer_phone , b.xcustomer_status ,a.remarks,c.referraler from " + tableName2 + " c left join  " + tableName1 + " a on a.id2=c.id" +
                "   left join pay_service_info d on c.pay_service_id=d.id left join " + this.getTableName() + " b on c.customer_id=b.id where 1=1 ";
        if (info.getStartArrivalTime() != null && info.getStartArrivalTime() != "") {
            sql += " and a.recharge_time >= '" + info.getStartArrivalTime() + "' ";
        }
        if (info.getEndArrivalTime() != null && info.getEndArrivalTime() != "") {
            sql += " and a.recharge_time <= '" + info.getEndArrivalTime() + "' ";
        }
        if (info.getXcustomerStatus() != null && info.getXcustomerStatus() != "") {
            sql += " and b.xcustomer_status = '" + info.getXcustomerStatus() + "'";
        }
        if (info.getFieldTc() != null && info.getFieldTc() != "") {
            sql += " and b.field_tc = '" + info.getFieldTc() + "'";
        }
        sql += " group by a.id2,b.reservations_id";
        sql += " union all select '', '合计', '','', '','','','','','','','','', '','',  '', '','' ,'',  sum(a.actual_account_balance), '','','', " +
                "   sum(case when a.checkout_method='现金' then  a.actual_account_balance else 0 end) as cash ,\n" +
                "   sum(case when a.checkout_method='扫码支付' then a.actual_account_balance else 0 end) as scavenging,\n" +
                "   sum(case when a.checkout_method='美团' then a.actual_account_balance else 0 end) as meiTuan,\n" +
                "   sum(case when a.checkout_method='分期贷款' then a.actual_account_balance else 0 end) as fqdk,\n" +
                "   sum(case when a.checkout_method='VIP消费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   (select sum(t.arrears) from " + tableName2 + " t left join (select id2, max(get_time) as getTime from " + tableName2 + " b   group by b.id2) t1\n" +
                "  on t.get_time=t1.getTime and t.id2=t1.id2 where 1=1  ";
        if (info.getStartArrivalTime() != null && info.getStartArrivalTime() != "") {
            sql += " and t.get_time >= '" + info.getStartArrivalTime() + "' ";
        }
        if (info.getEndArrivalTime() != null && info.getEndArrivalTime() != "") {
            sql += " and t.get_time <= '" + info.getEndArrivalTime() + "' ";
        }
        sql += ") as arrears,\n" +
                "   sum(case when a.checkout_method='分期收费' then a.actual_account_balance else 0 end) as fqsf,\n" +
                "   sum(case when a.checkout_method='VIP收费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   sum(case when a.checkout_method='积分抵扣' then a.actual_account_balance else 0 end) as integralDeduction,\n" +
                "   sum(case when a.checkout_method='华荣时代' then a.actual_account_balance else 0 end) as huaRong, \n" +
                "   sum(case when a.checkout_method='代金券' then a.actual_account_balance else 0 end) as voucher,\n" +
                "   sum(case when a.checkout_method='医保' then a.actual_account_balance else 0 end) as medicalInsurance,\n" +
                "   sum(case when a.checkout_method='糯米' then a.actual_account_balance else 0 end) as nuoMi ," +
                "   sum(case when a.checkout_method='银行卡' then a.actual_account_balance else 0 end) as payByCard ," +
                "   sum(case when a.checkout_method='百度钱包' then a.actual_account_balance else 0 end) as baiduqb," +
                "   sum(case when a.checkout_method='社保卡' then a.actual_account_balance else 0 end) as shebao, '', '','','','' " +
                "  from " + tableName1 + " a left join " + this.getTableName() + " b on a.customer_id=b.id    where 1=1 ";
        if (info.getStartArrivalTime() != null && info.getStartArrivalTime() != "") {
            sql += " and a.recharge_time >= '" + info.getStartArrivalTime() + "' ";
        }
        if (info.getEndArrivalTime() != null && info.getEndArrivalTime() != "") {
            sql += " and a.recharge_time <= '" + info.getEndArrivalTime() + "' ";
        }
        if (info.getXcustomerStatus() != null && info.getXcustomerStatus() != "") {
            sql += " and b.xcustomer_status = '" + info.getXcustomerStatus() + "'";
        }
        if (info.getFieldTc() != null && info.getFieldTc() != "") {
            sql += " and b.field_tc = '" + info.getFieldTc() + "'";
        }
        return sqlMapper.selectList(sql, PaymentTongJi.class);
    }

    public PaymentTongJi paymentRecordExportTotal(PaymentTongJi info) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        String tableName2 = tableHead + "deal_info";
        String sql = "  select '', '合计', '','', '','','','','','','','','', '','',  '', '','' ,'',  sum(a.actual_account_balance), '','','', " +
                "   sum(case when a.checkout_method='现金' then  a.actual_account_balance else 0 end) as cash ,\n" +
                "   sum(case when a.checkout_method='扫码支付' then a.actual_account_balance else 0 end) as scavenging,\n" +
                "   sum(case when a.checkout_method='美团' then a.actual_account_balance else 0 end) as meiTuan,\n" +
                "   sum(case when a.checkout_method='分期贷款' then a.actual_account_balance else 0 end) as fqdk,\n" +
                "   sum(case when a.checkout_method='VIP消费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   (select sum(t.arrears) from " + tableName2 + " t left join (select id2, max(get_time) as getTime from " + tableName2 + " b   group by b.id2) t1\n" +
                "  on t.get_time=t1.getTime and t.id2=t1.id2 where 1=1  ";
        if (info.getStartArrivalTime() != null && info.getStartArrivalTime() != "") {
            sql += " and t.get_time >= '" + info.getStartArrivalTime() + "' ";
        }
        if (info.getEndArrivalTime() != null && info.getEndArrivalTime() != "") {
            sql += " and t.get_time <= '" + info.getEndArrivalTime() + "' ";
        }
        sql += ") as arrears,\n" +
                "   sum(case when a.checkout_method='分期收费' then a.actual_account_balance else 0 end) as fqsf,\n" +
                "   sum(case when a.checkout_method='VIP收费' then a.actual_account_balance else 0 end) as VIPSF,\n" +
                "   sum(case when a.checkout_method='积分抵扣' then a.actual_account_balance else 0 end) as integralDeduction,\n" +
                "   sum(case when a.checkout_method='华荣时代' then a.actual_account_balance else 0 end) as huaRong, \n" +
                "   sum(case when a.checkout_method='代金券' then a.actual_account_balance else 0 end) as voucher,\n" +
                "   sum(case when a.checkout_method='医保' then a.actual_account_balance else 0 end) as medicalInsurance,\n" +
                "   sum(case when a.checkout_method='糯米' then a.actual_account_balance else 0 end) as nuoMi ," +
                "   sum(case when a.checkout_method='银行卡' then a.actual_account_balance else 0 end) as payByCard ," +
                "   sum(case when a.checkout_method='百度钱包' then a.actual_account_balance else 0 end) as baiduqb," +
                "   sum(case when a.checkout_method='社保卡' then a.actual_account_balance else 0 end) as shebao, '', '','','','' " +
                "  from " + tableName1 + " a left join " + this.getTableName() + " b on a.customer_id=b.id    where 1=1 ";
        if (info.getStartArrivalTime() != null && info.getStartArrivalTime() != "") {
            sql += " and a.recharge_time >= '" + info.getStartArrivalTime() + "' ";
        }
        if (info.getEndArrivalTime() != null && info.getEndArrivalTime() != "") {
            sql += " and a.recharge_time <= '" + info.getEndArrivalTime() + "' ";
        }
        return sqlMapper.selectOne(sql, PaymentTongJi.class);
    }


    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}


